// import qqmapwx from "../../qqmap-wx-jssdk.min"
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    currentTimeStamp:0,
    longitude:108.952,
    latitude:34.223,
    m1:0,//偏移的距离
    s1:0,//定位位置距离设定位置的距离
    m2:0,//偏移的距离
    s2:0,//定位位置距离设定位置的距离
    m3:0,//偏移的距离
    s3:0,//定位位置距离设定位置的距离
    m4:0,//偏移的距离
    s4:0,//定位位置距离设定位置的距离
    circles: [],
    markers:[],
    value1:[],
    openid:"",
    device_code:"",
    list:{},
    intervals: [],
    name:"暂无",
    age:"暂无",
    img:"",
    number11:[0,100,300,500,1000],
    index1:0,
    show:0,

    inter3: '',
   loc:""

  },
  onLoad: function () {
    const that =this
    let currentTimeStamp = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0,8)
    this.setData({
      currentTimeStamp: currentTimeStamp,

    });
    // 导航栏
    // 1.获取屏幕可使用宽度
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    // 2.获取状态栏高度
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight;
    //3.获取胶囊按钮位置信息
    const menuButton = wx.getMenuButtonBoundingClientRect();
    this.setData({
      navtop:menuButton.top
    })
    //获取自身定位
    
    function firstFunction() {
      return new Promise((resolve) => {
        // 执行 setData 操作

        
      });
    }

  },
  onReady(){
    
  },
  onShow(){
    
    this.mapCtx = wx.createMapContext('map')
    const app =getApp()
    const that =this
    wx.request({
      url: 'https://dj.ifankui.com/api/usr/getProfile',
      method:'POST',
      data:{
        openid:app.globalData.openid,
        
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success(res){

        console.log(res);
        console.log(res.data.data.headimg);
        that.setData({
          img:res.data.data.headimg,
        },()=>{
          if(res.data.data.lrxm.length==0){
            return
          }
          that.setData({
            name:res.data.data.lrxm,
            age:app.globalData.age
          })
        })
      }
    })
    wx.getLocation({
      type: 'gcj02',
      altitude:true,
      success:(res)=>{
        console.log(res);
      that.setData({
          longitude:res.longitude,
          latitude:res.latitude,
        })
      }
    })
    app.fetchData().then(data => {
      // 数据请求成功，可以在这里进行页面加载操作
      wx.request({
        url: 'https://dj.ifankui.com/api/usr/deviceList',
        method:"POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data:{
          openid:app.globalData.openid,
        },
        success(res){
          if(res.data.data.length==0){
            console.log(that.data.name);
            return
          }
          console.log(res);
          that.setData({
            device_code:res.data.data[0].device_code
          },()=>{
            that.startInter3()
            wx.request({
              url: 'https://dj.ifankui.com/api/device/getWeilan',
              method:"POST",
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              data:{
                
                device_code:that.data.device_code,
              },
              success(res){
                console.log(res);
                that.setData({
                  list:res.data.weilan
                  
                },()=>{
                  wx.request({
                    url: 'https://dj.ifankui.com/api/device/getDeviceInfo',
                    method:"POST",
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data:{
                      openid:that.data.openid,
                      device_code:that.data.device_code,
                    },
                    success(res){
                      console.log(res);
                      that.setData({
                        loc:res.data.device.last_position[0]
                      },()=>{
                        const lod01= that.data.loc.loc
                        console.log(lod01);
                        const lod02 =lod01.split(",");
                        let newLatitude1=lod02[1]
                        let newLongitude1=lod02[0]
                        
      const newMarker1 = {
        id:123, // 使用当前时间戳作为标记的唯一 ID
        latitude: newLatitude1,
        longitude: newLongitude1,// 替换为你的标记图标路径
        iconPath:'/images/666.png',
        width: 20,
        height: 30,
        title: '点击位置',
        callout: {
          content: that.data.name, // 标记内容
          color: '#000000', 
          fontSize: 14, 
          borderRadius: 5, 
          bgColor: '#ffffff', 
          padding: 5,
          display: 'ALWAYS' // 始终显示文字标签
        }
      };
      const markers = that.data.markers.concat(newMarker1);
      that.setData({
        
        markers: markers
      },()=>{
        console.log(that.data.markers);
      });
                      })
                    }
                  })
                })
                
      for(let i=0;i<res.data.weilan.length;i++){
        
        
        const lod = res.data.weilan[i].zuobiao
        let lod1 = lod.split(",");
        let newLatitude=lod1[1]
        let newLongitude=lod1[0]
    
      // 创建新的圆形标记对象
      const newCircle = {
        latitude: newLatitude,
        longitude: newLongitude,
        color: '#FF4500DD',
        radius:  that.data.list[0].size ,// 半径
        fillColor: '#00FF0055'
      };

      // 将新的圆形标记和标记对象添加到数据中

      const circles = that.data.circles.concat(newCircle);
      
      
    

      
      // 更新数据
      that.setData({
        circles: circles,
        show:that.data.list[0].size
      },()=>{
        console.log(that.data.markers);
      
        
      });
      
      
      }
      
              }
            })  
          })
        }
      })
    })

  },
  onHide(){
const that =this
    setTimeout(function() {
      that.setData({
        value1:[],
        device_code:"",
        circles: [],
        markers:[],
        index1:0,
        show:0,
      })
    }, 1000); // 2 秒后执行匿名函数

  },
  //点击获取点击位置的坐标，圆形
  getgps(e){
    const that =this
    const app =getApp()
    if(this.data.circles.length == 1){
      return;
    }
    if(that.data.name.length<=0){
      return;
    }
    console.log("点击地图位置坐标：", e.detail.latitude, e.detail.longitude);

    if(that.data.show!=0){    wx.request({
      url: 'https://dj.ifankui.com/api/device/addWeilan',
      method:'POST',
      data:{
        
        device_code:this.data.device_code,
        zuobiao: e.detail.longitude+","+e.detail.latitude,
        size:that.data.show
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },success(res){
        console.log(res);
        wx.request({
          url: 'https://dj.ifankui.com/api/device/getWeilan',
          method:"POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          data:{
            openid:app.globalData.openid,
            device_code:that.data.device_code,
          },success(res){
            
            that.setData({
              list:res.data.weilan
            })
          }
        })
      }

    })
    // 获取点击位置的经纬度
    const newLatitude = e.detail.latitude;
    const newLongitude = e.detail.longitude;
    
    // 创建新的圆形标记对象
    const newCircle = {
      latitude: newLatitude,
      longitude: newLongitude,
      color: '#FF4500DD',
      radius: that.data.number11[that.data.index1], // 半径
      fillColor: '#00FF0055'
    };

    

    // 将新的圆形标记和标记对象添加到数据中
    const circles = this.data.circles.concat(newCircle);
  

    // 更新数据
    this.setData({
      circles: circles,
    });
    }

    console.log(this.data.circles.length);

    
  },
  getwornning(){
    const that =this
    wx.request({
      url: 'https://dj.ifankui.com/api/device/getWeilanAlarm',
      method:'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data:{
        device_code:that.data.device_code,

        
      },
      success(res){
        console.log(res);
        that.setData({
            value1:res.data.data
            
        },()=>{

          
        })
      }
    })
  },
  startInter3: function () {
    var that = this;
    that.data.inter3 = setInterval(
      function () {
        that.getwornning()
        console.log('setInterval 每过 500 毫秒执行一次任务');
      },
      10000
    );
  },



  delall(){
    const app =getApp()
    const that =this
    for(let i=0;i<this.data.list.length;i++){
      console.log(this.data.list[i].id);
      wx.request({
        url: 'https://dj.ifankui.com/api/device/delWeilan',
        method:'POST',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data:{
          device_code:this.data.device_code,
          idstr:this.data.list[i].id,
        },
        success(res){
          console.log(res);

        }
      })
      const startTime = Date.now();
      while (Date.now() - startTime < 300) {
        // 空循环，什么都不做
      }
    
    }
    that.setData({
      circles: [],
      markers:[],
      show:0,
      index1:0
    },()=>{
      that.onShow()
    })

  },
  showActionSheet() {
    const that = this
    wx.showActionSheet({
      itemList: ["0米",'100米','300米','500米','1000米'],
      success(res) {
        console.log(res.tapIndex) // 用户点击的按钮序号，从0开始
        that.setData({
          index1:res.tapIndex,
          show:that.data.number11[res.tapIndex]
          
        },()=>{
          console.log(that.data.index1,that.data.show);
        })
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  tiao(){

    wx.navigateTo({
      url: '../user/user',
    })
  }


})